﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebPrice.Models;
using WebPrice.DAL;

namespace WebPrice.Controllers
{
    public class InstrumentController : Controller
    {
       // private PricingDbContext db = new PricingDbContext();

     private IInstrumentRepository instrumentRepository;

      public InstrumentController()
      {
          this.instrumentRepository = new InstrumentRepository(new PricingDbContext());
      }

      public InstrumentController(IInstrumentRepository instrumentRepository)
      {
         this.instrumentRepository = instrumentRepository;
      }


        // GET: /Instrument/
        public ActionResult Index()
        {
            return View(instrumentRepository.GetInstruments());
        }

        // GET: /Instrument/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Instrument instrument = instrumentRepository.GetInstrumentByID(id.Value);
            if (instrument == null)
            {
                return HttpNotFound();
            }
            return View(instrument);
        }

        // GET: /Instrument/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Instrument/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Id,InstrumentTypeId,Name,Active")] Instrument instrument)
        {
            if (ModelState.IsValid)
            {
                instrumentRepository.InsertInstrument(instrument);
                instrumentRepository.Save();
                return RedirectToAction("Index");
            }

            return View(instrument);
        }

        // GET: /Instrument/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Instrument instrument = instrumentRepository.GetInstrumentByID(id.Value);
            if (instrument == null)
            {
                return HttpNotFound();
            }
            return View(instrument);
        }

        // POST: /Instrument/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="Id,InstrumentTypeId,Name,Active")] Instrument instrument)
        {
            if (ModelState.IsValid)
            {
                instrumentRepository.UpdateInstrument(instrument);
                instrumentRepository.Save();
                return RedirectToAction("Index");
            }
            return View(instrument);
        }

        // GET: /Instrument/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Instrument instrument = instrumentRepository.GetInstrumentByID(id.Value);
            if (instrument == null)
            {
                return HttpNotFound();
            }
            return View(instrument);
        }

        // POST: /Instrument/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Instrument instrument = instrumentRepository.GetInstrumentByID(id);
            instrumentRepository.DeleteInstrument(id);
            instrumentRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                instrumentRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
